﻿using System;
using System.Linq;
using System.Windows.Forms;
using PersonalAccoutingProgram.Data;

namespace PersonalAccoutingProgram.View
{
    public partial class EditIncomes : Form
    {
        private string username;

        public EditIncomes(string username)
        {
            InitializeComponent();
            InitializeGridView(username);
            this.username = username;      
        }

        private void EditIncomes_Load(object sender, EventArgs e)
        {
        }

        private void InitializeGridView(string username)
        {
            IncomeDataClassesDataContext de =
            new IncomeDataClassesDataContext();
            var incomesView =
            from currentIncomes in de.GetTable<IncomesView>()
            where username == currentIncomes.Username
            select
            new
            {
                ID = currentIncomes.IncomesID,
                Origin = currentIncomes.OriginName,
                Date = currentIncomes.IncomesDate,
                Amount = currentIncomes.IncomesAmount
            };

            dataGridView.DataSource = incomesView;
            dataGridView.Columns[0].Visible = false;
        }

        private void dataGridView_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int gridID = (int)dataGridView.SelectedRows[0].Cells[0].Value;
            string gridOrigin = dataGridView.SelectedRows[0].Cells[1].Value.ToString();
            DateTime gridDate = (DateTime)dataGridView.SelectedRows[0].Cells[2].Value;
            decimal gridAmount = (decimal)dataGridView.SelectedRows[0].Cells[3].Value;  
            EditIncomesForm newEditForm =
            new EditIncomesForm(gridID, gridDate, gridAmount, gridOrigin);
            newEditForm.ShowDialog();
            InitializeGridView(username);
        }
    }
}